﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LX.Core.Utils;
using LX.Web.Mvc;
using S4Store.Service;
using S4Store.Domain;
using S4Store.Models;
using S4Store.Models.AfterSaleCarKPI;


namespace S4Store.Controllers
{
    public class AfterSaleDecoratingKPIController : ReportBaseController
    {
        //
        // GET: /AfterSaleDecoratingKPI/
        public CarService CarService { get; set; }
        public AfterSaleCarKPIService AfterSaleCarKPIService { get; set; }

        public ActionResult Index()
        {
            AfterSaleCarDecoratingIndexModel model = new AfterSaleCarDecoratingIndexModel();
            using (var t = new Transtion())
            {
                t.Inject(AfterSaleCarKPIService);
                var data = AfterSaleCarKPIService.GetAfterSaleCarDecoratingDayDataQuery().Where(x => x.StoreId == Store.Id).ToList();
                model.MonthDayData = data;
                return View(model);
            }
        }

        public ActionResult MonthTd(int y, int m)
        {
            using (var t = new Transtion())
            {
                t.Inject(AfterSaleCarKPIService);
                var config = AfterSaleCarKPIService.GetAfterSaleCarDecoratingMonthConfigs(y, m, Store.Id).FirstOrDefault();
                if (config == null)
                {
                    config = new Domain.AfterSaleKpi.AfterSaleCarDecoratingMonthConfig();
                    config.StoreId = Store.Id;
                    config.AddTime = new DateTime(y, m, 1);
                }
                return View(config);
            }
        }

        public ActionResult MonthConfig(int y, int m)
        {
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                t.Inject(AfterSaleCarKPIService);
                AfterSaleCarDecoratingListModel model = new AfterSaleCarDecoratingListModel();
                var brands = CarService.GetEnableCarBrand(Store.PinPaiId);
                model.CarBrands = brands.ToList();
                model.AfterSaleCarDecoratingMonthConfigs = AfterSaleCarKPIService.GetAfterSaleCarDecoratingMonthConfigs(y, m, Store.Id);
                model.Date = new DateTime(y, m, 1);
                model.isconfirmed = model.AfterSaleCarDecoratingMonthConfigs.Where(o => o.IsConfirmed).Count() > 0;
                return View(model);
            }
        }

        public ActionResult InputDayData(int y, int m, int d)
        {
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                t.Inject(AfterSaleCarKPIService);
                AfterSaleCarDecoratingListModel model = new AfterSaleCarDecoratingListModel();
                var brands = CarService.GetEnableCarBrand(Store.PinPaiId);
                model.CarBrands = brands.ToList();
                model.AfterSaleCarDecoratingDayInputs = AfterSaleCarKPIService.GetAfterSaleCarDecoratingDayInputs(y, m, d, Store.Id);
                model.Date = new DateTime(y, m, d);
                model.isconfirmed = model.AfterSaleCarDecoratingDayInputs.Where(o => o.IsConfirmed).Count() > 0;
                return View(model);
            }
        }

        public JsonResult ModifyBackInputAfterSaleDecoratingMonthConfig(IList<S4Store.Domain.AfterSaleKpi.AfterSaleCarDecoratingMonthConfig> afterSaleCarlist, int y, int m, bool isconfirm = false)
        {
            using (var t = new Transtion())
            {
                t.Inject(AfterSaleCarKPIService);
                DateTime now = new DateTime(y, m, 1);
                int sid = CurrentUser.StoreId.Value;
                if (CurrentUser.Position == Position.SystemManager)
                {
                    sid = Store.Id;
                }
                var list = new List<S4Store.Domain.AfterSaleKpi.AfterSaleCarDecoratingMonthConfig>();
                foreach (var model in afterSaleCarlist)
                {
                    model.AddTime = now;
                    model.StoreId = sid;
                    model.IsConfirmed = isconfirm;
                    list.Add(model);
                }
                AfterSaleCarKPIService.SubmitAfterSaleCarDecoratingMonthConfigList(now.Year, now.Month, sid, list);
                t.Commit();
            }

            return new JsonResult
            {
                Data = new
                {
                    success = true
                }
            };
        }


        public JsonResult ModifyBackInputAfterSaleDecoratingDayInput(IList<S4Store.Domain.AfterSaleKpi.AfterSaleCarDecoratingDayInput> afterSaleCarlist, int y, int m, int d, bool isconfirm = false)
        {
            using (var t = new Transtion())
            {
                t.Inject(AfterSaleCarKPIService);
                DateTime now = new DateTime(y, m, d);
                int sid = CurrentUser.StoreId.Value;
                if (CurrentUser.Position == Position.SystemManager)
                {
                    sid = Store.Id;
                }
                var list = new List<S4Store.Domain.AfterSaleKpi.AfterSaleCarDecoratingDayInput>();
                foreach (var model in afterSaleCarlist)
                {
                    model.AddTime = now;
                    model.StoreId = sid;
                    model.IsConfirmed = isconfirm;
                    list.Add(model);
                }
                AfterSaleCarKPIService.SubmitAfterSaleCarDecoratingDayDataList(now.Year, now.Month, now.Day, sid, list);
                t.Commit();
            }

            return new JsonResult
            {
                Data = new
                {
                    success = true
                }
            };
        }
    }
}
